package com.homework.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.UserBean;
import com.homework.service.UserService;
import com.homework.util.Constants;

public class RegisterAction {

	private String username;
	private String realname;
	private String password;
	private String phone;
	private String email;
	private String roleId;
	private String institute;
	private String grade;
	private String major;
	private String classes;
	private String university;

	
	private UserService userService;

	UserBean userBean = new UserBean();

	public RegisterAction() {
		userService = new UserService();
	}

	public String execute() {
		//数据库的配置文件加载：
		//connDB.loadProperty();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		userBean.setUsername(this.getUsername());
		userBean.setRealname(this.getRealname());
		userBean.setPassword(this.getPassword());
		userBean.setPhone(this.getPhone());
		userBean.setEmail(this.getEmail());
		userBean.setClasses(this.getClasses());
		userBean.setGrade(this.getGrade());
		userBean.setInstitute(this.getInstitute());
		userBean.setMajor(this.getMajor());
		userBean.setRoleId(this.getRoleId());
		userBean.setUniversity(this.getUniversity());

		try {

			// 注册业务
			int userId = userService.regist(userBean);
			if (userId != 0) {
				
				request.setAttribute("message", Constants.MESSAGE_SUCCESS_RegisetSuccess);
				return "Success";
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("message", Constants.MESSAGE_ERROR_RegisetError);
		return "Error";

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getInstitute() {
		return institute;
	}

	public void setInstitute(String institute) {
		this.institute = institute;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String getClasses() {
		return classes;
	}

	public void setClasses(String classes) {
		this.classes = classes;
	}
	
	public String getUniversity() {
		return university;
	}

	public void setUniversity(String university) {
		this.university = university;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}


}
